package com.atguigu.lease.web.admin.config;


import com.atguigu.lease.model.enums.ReleaseStatus;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfigs implements WebMvcConfigurer {
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory((new StringToReleaseStatusConverterFactory()));
    }
    static class StringToReleaseStatusConverterFactory implements ConverterFactory<String, ReleaseStatus> {
        @Override
        public <T extends ReleaseStatus> Converter<String, T> getConverter(Class<T> targetType) {
            return new Converter<String, T>() {
                @Override
                public T convert(String source) {
                    if (ReleaseStatus.RELEASED.getCode()==Integer.parseInt(source)){
                        return (T) ReleaseStatus.RELEASED;
                    }else {
                        return (T) ReleaseStatus.NOT_RELEASED;

                    }
                }
            };
        }
    }
}
